package com.ruoyi.flow.flow.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.flow.activiti.model.editor.ModelRepresentation;
import com.ruoyi.flow.activiti.security.SecurityUtils;
import com.ruoyi.flow.activiti.service.api.ModelService;
import com.ruoyi.flow.core.response.Response;
import com.ruoyi.flow.flow.model.ModelBean;
import com.ruoyi.flow.flow.workflow.FlowService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用于管理可部署的模块
 *
 * @Author: wtian
 * @Description:
 * @Date: Created in 10:21 2018/8/13
 * @Version: 1.0
 */
@Controller
@RequestMapping(value = "/flow/models")
public class ModelController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ModelController.class);
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ModelService modelService;
    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView toListPage() {
        ModelAndView modelAndView = new ModelAndView("admin/model/index");
        return modelAndView;
    }


    /**
     * 获取模型列表
     *
     * @param deployed 是否部署。0：全部；1：未部署；2：已部署
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<com.ruoyi.flow.flow.model.ModelBean> list(@RequestParam int deployed) {
        List<ModelBean> models = FlowService.modelService().list(deployed);
        return models;
    }

    @RequestMapping(value = "/{modelId}", method = RequestMethod.PUT)
    @ResponseBody
    public String deployModel(@PathVariable String modelId) {
        User user = SecurityUtils.getCurrentUserObject();
        return FlowService.modelService().deploy(modelId, user);
    }

    /**
     * 基于模型标识删除模型数据
     *
     * @param definitionId
     * @return
     */
    @RequestMapping(value = "/{definitionId}", method = RequestMethod.DELETE)
    @ResponseBody
    @Transactional
    public boolean delete(@PathVariable String definitionId) {
        return FlowService.modelService().deleteModel(definitionId);
    }

    /**
     * 跳转到模型创建页面
     *
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView toCreatePage() {
        ModelAndView modelAndView = new ModelAndView("admin/model/create");
        return modelAndView;
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Response<ModelRepresentation> create(ModelRepresentation representation) {
//        @RequestParam String name, @RequestParam String key, @RequestParam Optional<String> description
        return Response.<ModelRepresentation>success().setData(new ModelRepresentation(FlowService.modelService().createModel(representation)));
    }

    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public ModelAndView toUploadPage() {
        ModelAndView modelAndView = new ModelAndView("admin/model/upload");
        return modelAndView;
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Response<ModelRepresentation> upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        ModelRepresentation representation = FlowService.modelService().upload(request, file);
        return Response.<ModelRepresentation>success().setData(representation);
    }
}
